<template>
   <div class="rank" ref="rank">
    <Scroll class="toplist" :data="topList">
      <ul>
        <li class="item" v-for="(item, index) of topList" :key="index" @click="selectItem(item)">
           <div class="icon">
            <img width="100" height="100" v-lazy="item.coverImgUrl"/>
          </div>
          <ul class="songlist">
            <li class="song" v-for="(song,index) of item.tracks" :key="index">
              <span>{{index + 1}}</span>
              <span>{{song.first}}-{{song.second}}</span>
            </li>
          </ul>
        </li>
      </ul>
      <div class="loading-container" v-show="!topList.length">
        <loading></loading>
      </div>
    </Scroll>
    <router-view></router-view>
  </div>
</template>

<script>
import Scroll from 'base/scroll/scroll'
import Loading from 'base/loading/loading'
import { getToplist } from 'api/ranking'
import {mapMutations} from 'vuex'
export default {
  components: {
    Scroll,
    Loading
  },
  data () {
    return {
      topList: [],

    }
  },
  created () {
    this._getToplist()
  },
  methods: {
    selectItem(item){
      this.$router.push({
        path: `/rank/${item.id}`
      })
      this.setTopList(item)
      console.log(item)
    },
    _getToplist () {
      getToplist().then(res => {
        this.topList = res.data.body.list.filter( item => {
          return item.tracks.length > 0
        })
      })
    },
    ...mapMutations({
      setTopList: 'SET_TOP_LIST'
    })
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"
  @import "~common/stylus/mixin"

  .rank
    position: fixed
    width: 100%
    top: 88px
    bottom: 0
    background $color-highlight-background
    .toplist
      height: 100%
      overflow: hidden
      .item
        display: flex
        margin: 0 20px
        padding-top: 10px
        padding-bottom 10px
        border-bottom 1px solid #424242
        height: 100px
        &:last-child
          padding-bottom: 20px
        .icon
          flex: 0 0 100px
          width: 100px
          height: 100px
        .songlist
          flex: 1
          padding: 0 20px
          height: 100px
          overflow: hidden
          background: $color-highlight-background
          color: $color-text-d
          font-size: $font-size-small
          .song
            no-wrap()
            line-height: 26px
      .loading-container
        position: absolute
        width: 100%
        top: 50%
        transform: translateY(-50%)
</style>
